package com.fzb.forum4j.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.fzb.forum4j.dao.api.IWebSiteDao;
import com.fzb.forum4j.po.WebSite;
import com.fzb.forum4j.service.api.IWebSiteService;
@Service("webSiteServiceImpl")

public class WebSiteServiceImpl extends BaseServiceImpl<WebSite> implements
		IWebSiteService {

	
	
	@Resource(name="webSiteDaoImpl")
	private IWebSiteDao webSiteDao;
	
	@Override
	public Map<String, String> initWebSite() {
		return webSiteDao.initWebSite();
	}

	@Override
	public Map<String, String> updateWebSite(Map<String, String> webSite) {
		List<String> names=new ArrayList<String>();
		
		for(Entry<String, String> param:webSite.entrySet())
		{
			names.add(param.getKey());
		}
		List<Map<String,Object>> web=webSiteDao.loadWebSite(names);
		for (Map<String, Object> map : web) {
			
			if(!(webSite.get(map.get("name")).equals(map.get("value")))){
				map.put("value", webSite.get(map.get("name")));
				log.info(map.get("value"));
			}
			else{
				map.clear();
			}
		}
		
		if(!web.isEmpty()){
			for (Map<String, Object> map : web) {
				if(!map.isEmpty()){
					WebSite tweb=new WebSite();
					tweb.setName(map.get("name").toString());
					log.info(map.get("id").toString());
					tweb.setSiteId(Integer.parseInt(map.get("id").toString()));
					tweb.setValue(map.get("value").toString());
					tweb.setStatus(true);
					getIbd().modifyEntity(tweb);
				}
			}
		}
		return webSite;
	}

}
